#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <inttypes.h>
#include <time.h>
#include <ptcParse.h>

void packSend(void *ptcPtr, const char *packStr)
{
    ptcOptSet485Addr(ptcPtr, (const uint8_t *)"1234");
    ptcOptSetCrypte(ptcPtr, ptcPsCryptNone);
    ptcOptSetUsrData(ptcPtr, (const uint8_t *)packStr, strlen(packStr));
    ptcOptFinPack(ptcPtr);
}
void parseRcv(void *ptcPtr, uint8_t *data, int dsize)
{
    // logdebug("input size=%d", dsize);
    for (int i = 0; i < dsize; i++)
    {
        if (ptcOptPsData(ptcPtr, data[i]) <= 0)
        {
            continue;
        }
        logdebug("%.*s", ptcOptGetDataSize(ptcPtr), ptcOptGetUsrData(ptcPtr));
        break;
    }
}

int main(int argsize, const char **args)
{
    char tmpPtcsnd[1024] = {};
    char tmpPtcRcv[1024] = {};
    char tmpstr[128];
    ptcOptInit(tmpPtcsnd, sizeof(tmpPtcsnd), ptcPsVers1);
    ptcOptInit(tmpPtcRcv, sizeof(tmpPtcRcv), ptcPsVers1);
    logdebug("remain bytes:%d", ptcOptGetRemainSize(tmpPtcsnd));
    for (int i = 0; i < 10; i++)
    {
        snprintf(tmpstr, sizeof(tmpstr), "lxin input val=%d", 10 - i);
        packSend(tmpPtcsnd, tmpstr);
        parseRcv(tmpPtcRcv, ptcOptGetPackPtr(tmpPtcsnd), ptcOptGetTotalSize(tmpPtcsnd));
    }
    return 0;
}